import cv2 as cv
import numpy as np

# ----------------------礼帽和黑帽-----------------
# 礼帽：提取主体部分，保留了小的杂质
# 黑帽：显示出那些缺失的洞，保留了边缘信息

# 1.礼帽
img = cv.imread('words.jpeg', 0)
kernel = np.ones((2, 2), np.uint8)

# 礼帽操作
Hat = cv.morphologyEx(img, cv.MORPH_TOPHAT, kernel) # 图像，操作类型，卷积核
cv.imshow('Original',img)
cv.imshow('Limao', Hat)
cv.waitKey(0)
cv.destroyAllWindows()  

# 2.黑帽
img = cv.imread('words2.jpeg', 0)
kernel = np.ones((5, 5), np.uint8)

# 黑帽操作
BlackHat = cv.morphologyEx(img, cv.MORPH_BLACKHAT, kernel) # 图像，操作类型，卷积核
cv.imshow('Original',img)
cv.imshow('BlackHat', BlackHat)
cv.waitKey(0)
